package org.thread.test02.demo04;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author CSY
 * @date 2025 - 03 - 07
 */
public class UserThread extends Thread{
    private String inFileName;
    private String outFileName;

    /**
     * 拷贝文件的状态
     */
    private FileData fileData;

    public UserThread(){}

    public UserThread(String inFileName, String outFileName, FileData fileData) {
        this.inFileName = inFileName;
        this.outFileName = outFileName;
        this.fileData = fileData;
    }

    @Override
    public void run() {
        try {
            FileInputStream fis = new FileInputStream(inFileName);
            FileOutputStream fos = new FileOutputStream(outFileName);
            File file = new File(inFileName);
            fileData.setTotalSize(file.length());
            int len;
            int sum = 0;
            byte[] bytes = new byte[1024];
            while((len = fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
                sum += len;
                fileData.setCopiedSize(sum);
            }
            System.out.println("当前进度：100%");
            fos.close();
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
